package com.quectel.taskserver.job.datacleaar;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.quectel.constant.BackupConfigConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.entranceguard.dto.mongo.MongoPersonAccessRecordDto;
import com.quectel.core.module.entranceguard.service.mongo.MongoPersonAccessRecordService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.kit.PageDeleteKit;
import com.quectel.util.kit.StorageKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @email ryan.ran@quectel.com
 * @date 2022/1/10 5:28 下午
 */
@Component
@Slf4j
public class PersonAccessRecordClearTask implements SimpleJob {

    @DubboReference
    private MongoPersonAccessRecordService personAccessRecordService;

    @Override
    public void execute(ShardingContext shardingContext) {

        log.info("人行事件清除开始执行");


        Date now = new Date();

        int retainDays = BackupConfigConstants.PERSON_ACCESS_RECORD_RETAIN_DAYS;
        Map<String, Object> conditions = new HashMap<>();
        //保存天数往前退一年
        conditions.put(
                SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(),
                DateUtils.format(DateUtil.offsetDay(now, -1 * (retainDays + 365)).toJdkDate(), DateUtils.FormatType.COMMON)
        );
        conditions.put(
                SystemConstants.TIME_SLOT_KEY_PAIR.getRight(),
                DateUtils.format(DateUtil.offsetDay(now, -1 * retainDays).toJdkDate(), DateUtils.FormatType.COMMON)
        );

        PageDeleteKit.pageDelete(rqs -> personAccessRecordService.queryList(rqs), conditions, this::batchDelete);

        log.info("人行事件清除执行结束");
    }

    /**
     * 批量删除
     *
     * @param list
     */
    private void batchDelete(List<MongoPersonAccessRecordDto> list) {
        for (MongoPersonAccessRecordDto personAccessRecordDto : list) {
            //清除图片
            try {
                if (StringUtils.isNotBlank(personAccessRecordDto.getSnapImages())) {
                    if (JSONUtil.isJsonArray(personAccessRecordDto.getSnapImages())) {
                        //json数组
                        JacksonUtils
                                .parseArray(personAccessRecordDto.getSnapImages(), String.class)
                                .forEach(StorageKit::deleteFile);
                    } else {
                        //url形式
                        StorageKit.deleteFile(personAccessRecordDto.getSnapImages());
                    }
                }
                if (StringUtils.isNotBlank(personAccessRecordDto.getSnapVideoes())) {
                    if (JSONUtil.isJsonArray(personAccessRecordDto.getSnapVideoes())) {
                        //json数组
                        JacksonUtils
                                .parseArray(personAccessRecordDto.getSnapVideoes(), String.class)
                                .forEach(StorageKit::deleteFile);
                    } else {
                        //url形式
                        StorageKit.deleteFile(personAccessRecordDto.getSnapVideoes());
                    }
                }
            } catch (Exception e) {
                log.error("删除人行数据出错", e);
            }
            personAccessRecordService.deleteById(personAccessRecordDto.getId());
        }
    }
}
